<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\DistributProfit;

class DistributProfitRepository
{
    /**
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return DistributProfit::create($params);
    }

    public function insertGetId(array $params)
    {
        return DistributProfit::insertGetId($params);
    }

    public function profitAll($distributId)
    {
        $sum = DistributProfit::where('to_distribut_id', $distributId)
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitByCreateTime($distributId, $startTime, $endTime)
    {
        $sum = DistributProfit::where('to_distribut_id', $distributId)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitByCreateTimeFromGroupId($distributId, $fromGroupId, $startTime, $endTime)
    {
        $sum = DistributProfit::where([
            'to_distribut_id' => $distributId,
            'from_group_id' => $fromGroupId,
        ])
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitAllFromGroupId($distributId, $fromGroupId)
    {
        $sum = DistributProfit::where([
            'to_distribut_id' => $distributId,
            'from_group_id' => $fromGroupId,
        ])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }
}